package com.ddx.pay;




import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * Created by Administrator on 2016/5/9.
 */
public class WeChatPayDto implements Serializable {
    private static final long serialVersionUID = 1L;
    private String appId;  //公众号id
    private String packages;//统一下单接口返回的prepay_id参数值，提交格式如：prepay_id=***
    private String nonceStr;//随机字符串，不长于32位。
    private String timeStamp;//当前的时间
    private String signType;//签名方式
    private String appkey;//wxapp专用,商户平台加密api的key
    private String partnerid;//wxapp专用  即mch_id
    private String notifyurl;//wxapp专用 回调地址.
    private String paySign;//签名
    private String tradeno;  // 支付流水号
    private String prepayid;//预支付id 和 packages里内容一致 app支付时用..
    private String wxtradetype;// h5:JSAPI , app:APP
    private String errmsg;//过程中的错误信息.
    private String mwebUrl;//过程中的错误信息.
    public void setPaySign(String paySign) {
        this.paySign = paySign;
    }

    @Override

    public String toString() {
        return "WeChatPayDto{" +
                "appId='" + appId + '\'' +
                ", packages='" + packages + '\'' +
                ", nonceStr='" + nonceStr + '\'' +
                ", timeStamp='" + timeStamp + '\'' +
                ", signType='" + signType + '\'' +
                ", appkey='" + appkey + '\'' +
                ", partnerid='" + partnerid + '\'' +
                ", notifyurl='" + notifyurl + '\'' +
                ", paySign='" + paySign + '\'' +
                ", tradeno='" + tradeno + '\'' +
                ", prepayid='" + prepayid + '\'' +
                '}';
    }
    public String getWxtradetype() {
        return wxtradetype;
    }

    public void setWxtradetype(String wxtradetype) {
        this.wxtradetype = wxtradetype;
    }
    public String getPrepayid() {
        return prepayid;
    }

    public void setPrepayid(String prepayid) {
        this.prepayid = prepayid;
    }

    public String getAppkey() {
        return appkey;
    }

    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }

    public String getPartnerid() {
        return partnerid;
    }

    public void setPartnerid(String partnerid) {
        this.partnerid = partnerid;
    }

    public String getNotifyurl() {
        return notifyurl;
    }

    public void setNotifyurl(String notifyurl) {
        this.notifyurl = notifyurl;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getTradeno() {
        return tradeno;
    }

    public void setTradeno(String tradeno) {
        this.tradeno = tradeno;
    }

    public void setTimeStamp(String timeStamp) {

        this.timeStamp = timeStamp;
    }

    public String getTimeStamp() {

        return timeStamp;
    }

    public String getAppId() {
        return appId;
    }

    public String getPackages() {
        return packages;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public String getSignType() {
        return signType;
    }

    public String getPaySign() {
        return paySign;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setPackages(String packages) {
        this.packages = packages;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    public String getMwebUrl() {
        return mwebUrl;
    }

    public void setMwebUrl(String mwebUrl) {
        this.mwebUrl = mwebUrl;
    }

    public  String generateH5PaySign() {

        if(StringUtils.isEmpty(prepayid))
        {
           return "请设定prepay_id和appid";
        }
        this.setSignType("MD5");
        this.setPackages("prepay_id=" +prepayid);
        //按照ASCII进行拼接
        String stringA = "appId=" + this.getAppId() +
                "&nonceStr=" + this.getNonceStr()+
                "&package=" + this.getPackages() +
                "&signType=" + this.getSignType()+
                "&timeStamp=" + this.getTimeStamp()
                + "&key=";
        //根据网店返回商户号
        stringA += this.getAppkey() ;
        MD5 md5 = new MD5();
        String paySign = md5.EncoderByMd5(stringA).toUpperCase();


        return paySign;

    }


    /**
     *

     * @return
     */
    public String generateAPPPaySign() {

        if(StringUtils.isEmpty(prepayid)||StringUtils.isEmpty(partnerid)||StringUtils.isEmpty(appkey))
        {
            return "请先设定prepayid,partnerid(mch_id),appkey,appid参数";
        }
        this.setPackages("Sign=WXPay");
        //按照ASCII进行拼接
        String stringA = "appid=" +this.getAppId() +// map.get("appid") +
                "&noncestr=" + this.getNonceStr()+// map.get("noncestr") +
                "&package=" +this.getPackages()+// map.get("package") +
                "&partnerid=" + this.getPartnerid()+// map.get("partnerid") +
                "&prepayid=" + this.getPrepayid()+// map.get("prepayid") +
                "&timestamp=" + this.getTimeStamp()+// map.get("timestamp") +
                "&key=" + this.getAppkey();// map.get("appkey");
        this.setErrmsg(stringA);
        //MD5
        MD5 md5 = new MD5();
        String sign =md5.EncoderByMd5(stringA).toUpperCase();// md5.getMD5ofStr(stringA).toUpperCase();
        stringA +="&sign="+sign;
        this.setErrmsg(stringA);
        return sign;
    }


}
